function [] = plot_GradeComparisonBeaches(grade,RWQname,year)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BAR PLOT COMPARISON OF ALL THE BEACHES 5-YR 95TH HAZEN PERCENTILE 
% ANNUAL GRADES  WITH COLOR CODED BAR ACCORDING TO ANNUAL GRADE 
%
% INPUT: - grade   : array of all the beaches 5-yr 95th hazen percentile
%                     annual grade  
%        - RWQname : name of site (string)as in dataset 'BEACHES' loaded
%                    from Bays_n_Beaches.mat
%        - year    : year as of start of season 
%
% EXAMPLE : load Bays_n_Beaches.mat
%           beaches = get_sitename('beach');  %get RWQ beaches names
%           plot_GradecomparisonBeaches(testdata.anngrade_all,...
%           beaches{1},2011) 
% 
%        
% Author: Benedicte Pasquer, IMOS/eMII (http://imos.org.au/)
% email: benedicte.pasquer@utas.edu.au
% May 2013   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


if ~ischar(RWQname),error('RWQname must be a string'),end
beaches = get_sitename('beach');
nbeach = length(beaches);


figure('position',[1 1 1500 900],'Color',[.9 .98 .98])

% BAR PLOT
bar_g =  bar(grade);
bar_child = get(bar_g,'Children');
set(bar_child,'CDataMapping','direct');
mycolor = [0 1 0; 1 1 0; 1 0 0]; 

% BAR COLOR CODED ACCORDING TO GRADE LEVEL
greenRange = 1*(grade<200);
redRange = 3*(grade>=500);
yellowRange = 2*~(greenRange+redRange);
index = greenRange + redRange + yellowRange;
set(bar_child, 'CData',index);
colormap(mycolor);

% YMAX ROUND TO LAST HUNDRED TICKMARCK 
ymax = max(grade) + 150;ymax = floor(ymax/100)*100;  
xmax = length(grade);


titre{1} = [num2str(year) '-' num2str(year+1) ' classification of ', RWQname]; 
titre{2} = ['compared to the Derwent beaches'];

title(titre,...
    'Fontname','arial','Fontsize',14,'Fontweight','bold',...
    'color',[0 0 .7]);

ylabel({'5-year 95^t^h Hazen percentile';'for Enterococci'},...
    'Fontname','arial','Fontsize',14,'Fontweight','bold')

set (gca,'Ylim',[0 ymax],...
    'XLim',[0 nbeach+1],'Xtick',1:nbeach,...
    'XTicklabel',beaches,...
    'Fontname','arial','Fontsize',14,'Fontweight','bold',...
    'Color', [.9 .98 .98],...
    'YColor', [0 0 .7], 'Xcolor', [0 0 .7],...
    'Position',[0.13 0.30 0.72 0.61])
  
hh = rotateXlabels(gca(),45);
set (hh,'Color',[0 0 .7])
% HIGHLIGHT THE NAME OF SITE OF INTEREST
ind = find(strcmp(RWQname,beaches)==1);
set (hh(ind),'Color','r')
set(gcf, 'InvertHardCopy', 'off');

% CREATE A SECOND AXIS FOR LEGEND DISPLAY  
ax2 = axes('Position',[0.85 0.30 0.12 0.61],...
'XAxisLocation','top',...
'YAxisLocation','left',...
'Color','none',...
'XColor','k','YColor','k',...
'YLim',[0 1],'Xlim',[0 .2],...
'Visible','off');

hr1 = rectangle('Position',[.035 .9  .07 .05],'Facecolor','g');
hr2 = rectangle('Position',[.035 .84  .07 .05],'Facecolor','y');
hr3 = rectangle('Position',[.035 .78  .07 .05],'Facecolor','r');
set([hr1 hr2 hr3],'Edgecolor','k','Parent',ax2)

ht1 = text(.13,.93,'GOOD');
ht2 = text(.13,.87,'FAIR');
ht3 = text(.13,.81,'POOR');
set([ht1 ht2 ht3],'Color',[0 0 .7], 'Fontname','arial','Fontsize',14,'Fontweight','bold')

figname = RWQname;
figname(isspace(RWQname)) = []; %REMOVE BLANK FROM FILENAME
saveas(gcf,strcat(figname,'_all'),'png')


